package cn.zzdt4j.auth.filter;

import cn.zzdt4j.auth.toolkit.AuthUtil;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * * When anonymous login is enabled, an error will be reported when viewing the current user information.
 * * Modify the URI to query the default administrator information.
 * * <p>
 * * before:zzdt4j/v1/cs/auth/users/info or zzdt4j/v1/cs/auth/users/info/xxx
 * * after:zzdt4j/v1/cs/auth/users/info/admin
 *
 * @author by <a href="mailto:ligang941012@gmail.com">gang.Li</a>
 * @since 2023/10/28 22:08
 */
public class RewriteUserInfoApiFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        final boolean enableAuthentication = AuthUtil.isEnableAuthentication();
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        final String path = httpServletRequest.getRequestURI();
        if (!enableAuthentication && path.contains("users/info")) {
            httpServletRequest.getRequestDispatcher("/zzdt4j/v1/cs/auth/users/info/admin").forward(servletRequest, servletResponse);
            return;
        }
        filterChain.doFilter(servletRequest, servletResponse);
    }
}
